package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.FactoryDao;
import cn.learn.domain.cargo.Factory;
import cn.learn.domain.cargo.FactoryExample;
import cn.learn.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private FactoryDao factoryDao;

    @Override
    public void save(Factory factory) {
        factory.setId(UUID.randomUUID().toString().replace("-", ""));
        factory.setState(0);
        factory.setCreateTime(new Date());
        factory.setUpdateTime(new Date());
        factoryDao.insertSelective(factory);
    }

    @Override
    public void update(Factory factory) {
        factory.setUpdateTime(new Date());
        factoryDao.updateByPrimaryKeySelective(factory);
    }

    @Override
    public void delete(String id) {
        factoryDao.deleteByPrimaryKey(id);
    }

    @Override
    public Factory findById(String id) {
        Factory factory = factoryDao.selectByPrimaryKey(id);
        return factory;
    }

    @Override
    public List<Factory> findAll(FactoryExample example) {
        List<Factory> factories = factoryDao.selectByExample(example);
        return factories;
    }

    @Override
    public PageInfo findAll(FactoryExample factoryExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Factory> factories = factoryDao.selectByExample(factoryExample);
        PageInfo pageInfo = new PageInfo(factories);
        return pageInfo;
    }
}
